#include "lexer.hpp"
#include "parser.hpp"
#include "codegen.hpp"
#include "codeobj.hpp"
#include "typechecker.hpp"
#include "symtable.hpp"

#include <iostream>
using namespace std;

void test_lexer(const char* filename) {
    auto lexer = Lexer::from_file(filename);
    Token* t = NULL;
    while ((t = lexer->next())) {
        t->print();
    }
}

void test_parser(const char* filename) {
    SymbolTable::builtin_symtable->load_builtin();
    SymbolTable symtable(SymbolTable::builtin_symtable);

    Parser parser(Lexer::from_file(filename), &symtable);
    Node* n = parser.parse();

    {
        Dumper d;
        d.visit(n);
        printf("\n");
    }

    // To fill symtable.
    TypeChecker type_checker(&symtable, false);
    type_checker.visit(n);

    CodeGen c(&symtable);
    c.visit(n);

    Disassembler dis;
    dis.dis(c.insts());

    CodeObject* co = c.make_code_object();;
    CodeObject::write_to_file(string(filename).append("c"), *co);
}

int main(int argc, char** argv) {
    if (argc != 2) {
        printf("Usage: galang filename\n");
        return -1;
    }

    test_parser(argv[1]);

    return 0;
}
